<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\Models\Agent as TAgent;
use Illuminate\Http\Request;
use App\Models\Level;
use App\Models\AgentLog;
use App\Models\Order;

class Agent extends AdminController{
    
    public function index(Request $req){
        $aid = $req->get('logaid');
        if ($aid > 0) {
            if($req->method() == 'POST'){
                $psize = 30;
                $list = AgentLog::orderByDesc('id')->paginate($psize);
                foreach ($list as &$v) {
                    $v->createtime = date('Y-m-d H:i',$v->createtime);
                    if ($v->type == 1) $v->remark = Order::where('id',$v->remark)->value('ordersn');
                    elseif ($v->type == 2) $v->remark = '在线充值';
                    elseif ($v->type == 3) $v->remark = $v->remark ? '升级等级' : '续费等级';
                }
                wxresp(1, ['rows' => $list->items(), 'total' => $list->total()]);
            }
            return $this->V("log",array('logaid'=>$aid));
        }
        if($req->method() == 'POST'){
            if ($req->post('op') == 'credit') {
                $credit = floatval($req->post('credit',0));
                if (empty($credit)) wxresp(0,'请输入变化余额');
                $remark = $req->post('remark','');
                AgentLog::creditPay($req->post('aid'), 0, $credit, $remark);
                wxresp(1);
            }
            $psize = 15;
            $list = TAgent::orderBy('id')->paginate($psize);
            foreach ($list as $v) {
                $v->num = TAgent::where('paid',$v->id)->count();
                $v->leveltitle = Level::where('level',$v->level)->value('title');
                if ($v->paid > 0){
                    $p = TAgent::find($v->paid);
                    if ($p) {
                        $v->ptitle = '';
                        if ($p->title) $v->ptitle = $p->title."<br>";
                        $v->ptitle .= $p->mobile;
                    }
                }
            }
            wxresp(1, ['rows' => $list->items(), 'total' => $list->total()]);
        }
        return $this->V("index",array('levels'=>Level::select('level','title')->get()));
    }
    
    public function del(Request $req){
        if ($req->method() == 'POST') {
            $id = $req->input('id', 0);
            $item = TAgent::find($id);
            if (!$item) wxresp(0, '代理不存在');
            TAgent::where('id', $id)->delete();
            wxresp(1);
        }
        wxresp(0);
    }
    
    public function post(Request $req){
        $id = $req->input("aid",0);
        $data = [
                'title' => $req->post('name'),
                'level' => $req->post('level'),
                'mobile' => $req->post('mobile'),
        ];
        if($req->post('passwd','') != '') $data['passwd'] = md5($req->post('passwd'));
        if ($id){
            TAgent::where('id', $id)->update($data);
        }else{
            if (TAgent::where('mobile',$data['mobile'])->count() > 0) wxresp(0,'手机号已注册');
            $data['code'] = TAgent::getUnicode();
            $id = TAgent::insertGetId($data);
        }
        wxresp(1);
    }
}